package com.whty.cms.filewps.util;

import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class FCUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(FCUtils.class);


    public static JSONObject postFileCenter(JSONObject json, String requestUrl) throws Exception {
		String url = "https://filecenter.huijiaoyun.com/filecenter/upload/"+requestUrl;
//        String url = "http://filecenter-test.tx.huijiaoyun.com/filecenter/upload/"+requestUrl;

        HttpClient httpClient = new HttpClient(url);
		httpClient.setRequestProperty("Content-Type", "application/json;charset=utf-8");
		String response = httpClient.doPost(json.toString());
		return JSONObject.fromObject(response);
	}


    
    public static String uploadCos(String host, String cosKey, String auth, InputStream in) throws Exception {
		String path = "http://"+host+"/"+cosKey;
		URL url = new URL(path);
        HttpURLConnection connection ;
        StringBuffer sbuffer=null;
        String fileMd5 = null;
        try {
        	//添加 请求内容
            connection= (HttpURLConnection) url.openConnection();
            //设置http连接属性
            connection.setDoOutput(true);// http正文内，因此需要设为true, 默认情况下是false;
            connection.setDoInput(true);// 设置是否从httpUrlConnection读入，默认情况下是true;
            connection.setRequestMethod("PUT"); // 可以根据需要 提交 GET、POST、DELETE、PUT等http提供的功能
            connection.setRequestProperty("Host", host);  //设置请 求的服务器网址，域名，例如***.**.***.***
            connection.setRequestProperty("Authorization", auth);
            connection.connect();            
            OutputStream out = connection.getOutputStream();//向对象输出流写出数据，这些数据将存到内存缓冲区中
            //DataInputStream in = new DataInputStream(part.getInputStream());
            int bytes = 0;
            byte[] bufferOut = new byte[1024];
            while ((bytes = in.read(bufferOut)) != -1) {
            	out.write(bufferOut, 0, bytes);
            }
            fileMd5 = DigestUtils.md5Hex(in);
            System.out.println(fileMd5);
            in.close();
            out.flush();     
            // 关闭流对象,此时，不能再向对象输出流写入任何数据，先前写入的数据存在于内存缓冲区中          
            out.close();           
            //读取响应           
            if (connection.getResponseCode()==200)            {
                // 从服务器获得一个输入流
            	InputStreamReader inputStream =new InputStreamReader(connection.getInputStream());//调用HttpURLConnection连接对象的getInputStream()函数, 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
            	BufferedReader reader = new BufferedReader(inputStream);

            	String lines;
            	sbuffer= new StringBuffer("");

            	while ((lines = reader.readLine()) != null) {                
            		lines = new String(lines.getBytes(), "utf-8");
            		sbuffer.append(lines);
            	}                
            	reader.close();      
            	LOGGER.info(lines);
            }else{          
            }    
            //断开连接           
            connection.disconnect();    
        } catch (IOException e) {
        	e.printStackTrace();     
        }
        return fileMd5;
	}

}
